package com.gitee.wsl.compose.modifier


import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.material3.ripple
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.semantics.Role
import com.gitee.wsl.compose.ext.input.detectTapGesturesIf

interface ModifierClickScope: ModifierScope {

    fun predicate(block:(Offset) -> Boolean)

    fun onDoubleClick(block:(Offset) -> Unit)

    fun onLongClick(block:(Offset) -> Unit)

    fun onClick(block:(Offset) -> Unit)

}

class ModifierClickScopeImp(override var modifier: Modifier): ModifierClickScope{
    var predicate: (Offset) -> Boolean = { true }
    var onDoubleTap: ((Offset) -> Unit)? = null
    var onLongPress: ((Offset) -> Unit)? = null
    var onTap: ((Offset) -> Unit)? = null
    override fun predicate(block: (Offset) -> Boolean) {
        predicate = block
    }

    override fun onDoubleClick(block: (Offset) -> Unit) {
        onDoubleTap = block
    }

    override fun onLongClick(block: (Offset) -> Unit) {
        onLongPress = block
    }

    override fun onClick(block: (Offset) -> Unit) {
        onTap = block
    }

    /*@SuppressLint("ModifierFactoryUnreferencedReceiver")
    fun build(key1: Any?=null){
        modifier = modifier.pointerInput(key1){
            detectTapGesturesIf(predicate = predicate,
                onDoubleTap = onDoubleTap,
                onLongPress = onLongPress,
                onTap = onTap)
        }
    }*/
}

fun Modifier.clickListener(
    key1: Any?=null,
    block: ModifierClickScope.()->Unit) = pointerInput(key1){
    with(ModifierClickScopeImp(modifier = this@clickListener).apply(block)){
        detectTapGesturesIf(predicate = predicate,
            onDoubleTap = onDoubleTap,
            onLongPress = onLongPress,
            onTap = onTap)
    }
}

/**
 * Add a clickable modifier that has no indication (no ripple)
 */
fun Modifier.noIndicationClickable(
    enabled: Boolean = true,
    onClickLabel: String? = null,
    role: Role? = null,
    interactionSource: MutableInteractionSource? = null,
    onClick: () -> Unit,
): Modifier = clickable(
    interactionSource = interactionSource,
    indication = null,
    enabled = enabled,
    onClickLabel = onClickLabel,
    role = role,
    onClick = onClick
)


@Composable
fun Modifier.clickableRipple(
    enabled: Boolean = true,
    onClick: () -> Unit
): Modifier =
    this.clickable(
        enabled = enabled,
        onClick = onClick,
        interactionSource = remember { MutableInteractionSource() },
        indication = ripple()
    )